package net.trustie.webmagic.utils;

import net.trustie.webmagic.one.Configure;

/*
 * 此类用于设置随机长时间段睡眠
 */
public class LongSleepHelper {
	private long lastSleep;//上一次长睡眠的结束时间
	private int sleepInterval;//下一次长睡眠间隔期	
	private int longSleepTime;//此次长睡眠的时间
	
	private int minlongSleepInterval;
	private int maxlongSleepInterval;
	private int minLongSleepTime;
	private int maxLongSleepTime;
	public LongSleepHelper(Configure conf){
		this.minlongSleepInterval = conf.getMinlongSleepInterval();
		this.maxlongSleepInterval = conf.getMaxlongSleepInterval();
		this.minLongSleepTime = conf.getMinlongSleepTime();
		this.maxLongSleepTime = conf.getMaxlongSleepTime();
	}
	//记录当前时间
	public void startMonitor(){
		lastSleep = System.currentTimeMillis();
		//sleepInterval = MyRandomer.getRandomNum(1*60*60*1000,2*60*60*1000);
		sleepInterval = MyRandomer.getRandomNum(this.minlongSleepInterval,this.maxlongSleepInterval);
	}
	public boolean timeToLongSleep(){
		long now = System.currentTimeMillis();
		if(now >= (lastSleep + sleepInterval)){	
			//longSleepTime = MyRandomer.getRandomNum(30*60*1000,2*60*60*1000);
			longSleepTime = MyRandomer.getRandomNum(this.minLongSleepTime,this.maxLongSleepTime);
			lastSleep = now + longSleepTime;
			//sleepInterval = MyRandomer.getRandomNum(1*60*60*1000,2*60*60*1000);
			sleepInterval = MyRandomer.getRandomNum(this.minlongSleepInterval,this.maxlongSleepInterval);
			return true;
		}else
			return false;
	}
	
	public int longsleepTime(){
		return longSleepTime;
	}
}
